// pages/addresspages/addressEditor/addressEditor.js
var Ajax = require('../../../utils/Ajax.js')
var utils = require('../../../utils/util.js');
var app = getApp();
import {
  AreaPicker
} from "../../../utils/addressseltel.js";

Page(Object.assign({}, AreaPicker, {

  /**
   * 页面的初始数据
   */
  data: {
    province: null, //省 {id,name}
    city: null, //市
    district: null, //县 区
    name: '',
    phone: '',
    address: '',
    selected: false,
    iseditor: true
  },
  oninput(e) { //获取输入的值
    var {
      name
    } = e.currentTarget.dataset;
    var { value } = e.detail;
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“/\"'。\\，、_？\\s+]")
    this.setData({
      [name]: value.replace(pattern, "")
    })
  },
  showNormSelectModal(e) {//显示弹出层
    var id = this.onSetid(e);
    var modal = this.selectComponent("#" + (id || 'modal'));
    if (modal.isShow()) {
      modal.hide()
    } else {
      modal.show();
    }
  },
  closeModal(e) {//关闭弹窗
    var id = this.onSetid(e);
    var modal = this.selectComponent("#" + (id || 'modal'));
    modal.hide();
  },
  onSetid(e) {
    var id = 'modal'
    if (typeof (e) == 'string') {
      id = e
    } else {
      id = e && (e.currentTarget.id || e.currentTarget.dataset.id);
    }
    console.log(id)
    return id;
  },
  oninpfocus(e) {//输入框获取焦点
    var inputname = e.target.dataset.name;
    this.setData({ atv: inputname })
  },
  oninpblur(e) {//输入框失去焦点
    var inputname = e.target.dataset.name
    this.setData({ atv: '' })
  },
  onsetChang(e) { //用户选择颜色规格及材质
    var setletindex = e.currentTarget.dataset;
    this.setData({
      ['' + setletindex.name]: setletindex.index
    })
  },
  onAreaCommit(locationList) { //当用户更换地区
    this.setData({
      province: locationList[0] || {},
      city: locationList[1] || {},
      district: locationList[2] || {},
    });
  },
  switchChange: function(e) {
    var { options}=this.data;
    if (options.selected=='0'){
      this.setData({
        selected:true
      },()=>{
        utils.showtoast('默认地址无法取消')
      })
      return;
    }
    this.setData({
      selected: e.detail.value
    })
  },
  delText(e) { //清空input
    this.setData({
      ['' + e.detail]: ''
    })
  },
  confirmSub(){
    var {
      options
    } = this.data;
    Ajax.post('deleteReceiptAddress',{
      id: options.id
    }).then((d) => {
      if (d.status == 200) {
        utils.showtoast('删除地址成功')
        wx.navigateBack()
      } else {
        utils.showtoast(d.data)
      }
    }).catch((e) => {
      console.log(e)
      utils.showtoast('网络错误,删除地址失败')
    })
  },
  onDeladdress() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否删除该地址',
      success(res) {
        if (res.confirm) {
          that.confirmSub()
        } else if (res.cancel) {
          that.onSaveaddress()
        }
      }
    })
    // this.showNormSelectModal('Popup');
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      options: options,
      name: options.name,
      phone: options.phone,
      address: options.address,
      province: {
        text: options.provinceName,
        value: options.province
      },
      city: {
        text: options.cityName,
        value: options.city
      },
      district: {
        text: options.areaName,
        value: options.area
      },
      selected: options.selected == 0 ? true : false,
      model: app.globalData.model,
    })
  },
  onSaveaddress() {
    var {
      name,
      phone,
      address,
      address,
      selected,
      options,
      province,
      city,
      district
    } = this.data
    this.setData({ iseditor: false })
    Ajax.post('editReceiptAddress',{
      id: options.id,
      name,
      phone,
      address,
      selected: selected ? 0 : 1,
      province: province.text,
      city: city.text,
      area: district.text || '',
      uid: utils.getUserInfo() && utils.getUserInfo().id
    }).then((d) => {
      if (d.status == 200) {
        utils.showtoast('编辑地址成功')
        wx.navigateBack()
        this.setData({ iseditor: true })
      } else {
        this.setData({ iseditor: true })
        utils.showtoast(d.data)
      }
    }).catch((e) => {
      console.log(e)
      this.setData({ iseditor: true })
      utils.showtoast('网络错误,编辑地址失败')
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var { options}=this.data;
    this.init(true,[options.province, options.city, options.area]);
    setTimeout(()=>{
      this.editor([options.province, options.city], [options.province, options.city, options.area])
    },200)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
}))